<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text" id="text">
    <button onclick="send()">发送</button>
    <script>
        let text = document.getElementById('text');
        //在浏览器里连接服务器的8888
        let socket = new WebSocket("ws://localhost:8888");
        //当连接打开或者说建立后会触发回调
        socket.onopen = function () {
            socket.send('server');
        }
        //当服务器端给客户端发消息后，客户端可以通过onmessage进行接收  event.data
        socket.onmessage = function (event) {
            console.log(event.data);
        }
        function send() {
            let value = text.value;
            socket.send(value);
            text.value = '';
        }
    </script>
</body>

</html>